<?php
$result = [
    'status' => 0,
    'data' => null,
    'message' => '未知错误'
];

include_once 'sql.php';
include_once 'sql-fun.php';

if (isset($sql) && $sql) {
    $params = [];
    $params['is'] = isset($_GET['is']) && ($_GET['is'] < 0) ? -1 : 1;
    $params['tablename'] = isset($_GET['name']) ? $_GET['name'] : null;
    if ($params['tablename']) {
        if ($params['is'] === 1) {
            $fun = 'create_' . $params['tablename'];
            $createUsers = $fun($mysql, $params);
            if ($createUsers === 1) {
                $result['status'] = 1;
                $result['data'] = true;
                $result['message'] = '创建 (' . $params["tablename"] .') 成功';
            }
            else {
                $result['status'] = 0;
                $result['data'] = false;
                $result['message'] = '创建 (' . $params["tablename"] .') 失败';
            };
        }
        else {
            $deleteTable = delete_table($mysql, $params);
            if ($deleteTable === 1) {
                $result['status'] = 1;
                $result['data'] = true;
                $result['message'] = '删除 (' . $params["tablename"] .') 成功';
            }
            else {
                $result['status'] = 0;
                $result['data'] = false;
                $result['message'] = '删除 (' . $params["tablename"] .') 失败';
            };
        };
    }
    else {
        $result['status'] = 0;
        $result['data'] = false;
        $result['message'] = '数据表名称为空';
    };

    // 关闭连接
    $mysql->close();
}
else {
    $result['message'] = '数据库连接错误';
};

echo json_encode($result);
?>